package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import dao.DAOContact;
import domain.Contact;
import domain.Entreprise;

/**
 * Servlet implementation class SearchEnterpriseServlet
 */

public class SearchEnterpriseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchEnterpriseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGetOrPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGetOrPost(request, response);
	}

	protected void doGetOrPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		// DAOContact dao = (DAOContact) context.getBean("contactDAO");

		String critere = (String) request.getParameter("typeFind");
		String key = (String) request.getParameter("key");

		request.setAttribute("listEntreprise",
				requestByCriteria(context, critere, key));

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("pages/listEnterprise.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * search a contact by criteria
	 * 
	 * @param context
	 * @param critere
	 * @param key
	 * @return
	 */
	public List<Entreprise> requestByCriteria(ApplicationContext context,
			String critere, String key) {
		DAOContact dao = (DAOContact) context.getBean("contactDAO");
		List<Contact> contacts = new ArrayList<Contact>();
		List<Entreprise> results = new ArrayList<Entreprise>();
		// in the case of all contact search: no criteria
		if (critere == null || critere.isEmpty() || key == null
				|| key.isEmpty())
			return dao.searchEnt();

		// search by criteria
		// if (critere.equals("ID")) {
		// Contact contact = dao.searchById(Integer.parseInt(key));
		// if (contact != null && !(contact instanceof Entreprise))
		// contacts.add(contact);
		// } else if (critere.equals("FirstName")) {
		// contacts = dao.searchByName(key);
		//
		// } else if (critere.equals("Email")) {
		// contacts = dao.searchByEmail(key);
		// } else if (critere.equals("tel")) {
		//
		// }
		// for (Contact contact : contacts) {
		// if (!(contact instanceof Entreprise))
		// results.add(contact);
		// }
		return results;

	}

}
